﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.zyy;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.WJL;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.WJL
{
    public class UpdateRevokecollectionCommandHanders : IRequestHandler<UpdateRevokecollectionCommands, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public UpdateRevokecollectionCommandHanders(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }


        /// <summary>
        /// 撤销回收信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(UpdateRevokecollectionCommands request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                // 1. 指定更新字段（关键修改）
                var list = db.Updateable<ReturnApplication>()
                    .SetColumns(x => x.Status == 2) // 明确指定更新字段
                    .Where(x => x.ReturnApplicationId == request.ReturnApplicationId) // 必须添加条件
                    .ExecuteCommand();

                // 2. 结果处理
                result.Code = list > 0 ? ApiEnums.修改成功 : ApiEnums.修改失败;
                result.Msg = list > 0 ? "修改成功" : "未找到匹配记录";
            }
            catch (Exception ex)
            {
                result.Code = ApiEnums.修改失败;
                result.Msg = $"更新失败：{ex.Message}";
            }

            return result;
        }
    }
}
